from django.shortcuts import render
from web.forms.project import S28
from django.http import JsonResponse


def statistics(request, project_id):
    return render(request, 'statistics.html')


# 这个视图函数专门管饼状图的数据
def statistics_pie(request, project_id):
    this_time = ''
    if request.GET.get('start') and request.GET.get('end'):
        print(request.GET.get('start'))
        print(request.GET.get('end'))
        this_time = 'and start_time between "{}" and "{}"'.format(request.GET.get('start'),request.GET.get('end'))
    s28 = S28().py_searche('select count(*),problem_type,'
                           '(select count(*) from problem) from problem '
                           'where project_id="{}" {}  group by problem_type '.format(project_id,this_time))

    ret = []
    for i in s28:
        ret.append({'name': i[1], 'y': int(i[0] / i[2] * 100)})
    return JsonResponse({'status': True, 'data': ret})



# 这个是专门管柱状体的

def statistics_column(request, project_id):
    # 获取get请求的参数
    this_time = ''
    if request.GET.get('start') and request.GET.get('end'):
        this_time = 'and start_time between "{}" and "{}"'.format(request.GET.get('start'), request.GET.get('end'))
    # 拿到了这个项目所派遣的所有的人的 id号 和 这个人的名字
    assign_user_information = S28().py_searche('select assign_user_information,user '
                                               'from problem  left join user_information '
                                               'on problem.assign_user_information=user_information.id '
                                               'where problem.project_id="{}" {}'
                                               'group by assign_user_information'.format(project_id, this_time))

    ret = []
    statuse_index = ['new', 'process', 'solve', 'ignore', 'feedback', 'close', 'reopen']
    for i in assign_user_information:
        s28 = S28().py_searche('select statused, count(*),assign_user_information'
                               ' from problem '
                               'where assign_user_information="{}"'
                               ' group by statused'.format(i[0]))
        dicts = {'name': i[1], 'data': [0, 0, 0, 0, 0, 0, 0]}
        for j in s28:
            dicts['data'][statuse_index.index(j[0])] = j[1]

        ret.append(dicts)

    return JsonResponse({'status': True, 'data': ret})

